package com.sky.service.impl;

import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishflavorsMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.Dishservice;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class Dishserviceimpl implements Dishservice {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishflavorsMapper dishflavorsMapper;
@Autowired
private SetmealDishMapper setmealDishMapper;
    @Override
    public void save(DishDTO dishdto) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishdto, dish);
        dishMapper.insertDish(dish);
        Long dishId = dish.getId();


        List<DishFlavor> dishFlavors = dishdto.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor df : dishFlavors) {
                df.setDishId(dishId);
                dishflavorsMapper.insertDish(df);
            }
        }

    }

    @Override
    public PageResult selectbypage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.selectbypage(dishPageQueryDTO);
        List<Dish> dishs = page.getResult();
        Long total = page.getTotal();
        PageResult pageResult = new PageResult(total, dishs);

        return pageResult;
    }

    @Override
    public DishVO selectbyid(Long id) {
//从菜品表里查询
        Dish dish = dishMapper.selectbyid(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);

        if (dishflavorsMapper.selectbyid(id) != null)
            for (DishFlavor df : dishflavorsMapper.selectbyid(id)) {
                dishVO.getFlavors().add(df);
            }


        return dishVO;

    }

    @Override
    public void updatedish(DishDTO dishdto) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishdto, dish);
        dishMapper.updatedish(dish);
        Long dishId = dish.getId();


        List<DishFlavor> dishFlavors = dishdto.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            dishflavorsMapper.delete(dishId);
            for (DishFlavor df : dishFlavors) {
                df.setDishId(dishId);


                dishflavorsMapper.insertDish(df);
            }

        }
    }

    @Override
    public void startandstop(Integer stats, Long id) {

        Dish dish=Dish.builder()
                .status(stats)
                .id(id).build();
        dishMapper.updatedish(dish);

    }

    @Override
    @Transactional
    public void delteBatch(List<Long> ids) {
        //判断是否起售，起售中不能删
        for (Long id : ids) {
            Dish dish = dishMapper.selectbyid(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
            //被套餐关联不能删
            List<Long> setmealids=setmealDishMapper.getSetmealDishIds(ids);
            if(setmealids!=null&&setmealids.size()>0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }

            dishMapper.deletebyids(ids);
            dishflavorsMapper.deletebyids(ids);




    }

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();



        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishflavorsMapper.selectbyid(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}